package com.dayuanit.handler;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dayuanit.exception.MallBusException;
import com.dayuanit.service.JedisService;
import com.dayuanit.service.OrderService;

@Component("orderQueueHandler")
public class OrderQueueHandler {
	
	private static final Logger logger = LoggerFactory.getLogger(OrderQueueHandler.class);
	
	@Autowired
	private JedisService jedisService;
	
	@Autowired
	private OrderService orderService;
	
	public void processOrder() {
		logger.info("定时任务获取队列开启...");
		while (true) {
			String msg = jedisService.popOrderQueue();
			
			if (StringUtils.isBlank(msg)) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
				}
				continue;
			}
			
			String[] orderMsg = msg.split("\\$");
			try {
				logger.info("orderMsg[0]=" + orderMsg[0]);
				orderService.orderPaySuccess(Integer.parseInt(orderMsg[0]));
			} catch (MallBusException me) {
				jedisService.pushOrderQueue(msg);
			} catch (Exception e) {
				logger.error("处理支付成功的订单失败{}", e.getMessage(), e);
				jedisService.pushOrderQueue(msg);
			}
		}
		
	}
}
